﻿using System;
using System.Reflection;
using System.Runtime.InteropServices.Expando;
using System.Runtime.InteropServices.ComTypes;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Net;
using dshow;
using dshow.Core;
using System.Linq;
using System.Text;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using BrainTechLLC;
using System.Configuration;
using System.Diagnostics;
using BrainTechLLC.ThreadSafeObjects;
using System.Windows.Forms;
using System.Windows.Threading;

namespace BrainTechLLC.WebCamLib3
{
    public class CameraHandlerViewModel : BaseNotifyPropertyChanged
    {
        protected CameraHandler _handler;
        protected DispatcherTimer _refreshTimer;

        public CameraHandlerViewModel(CameraHandler handler)
        {
            _handler = handler;
            _handler.PropertyChanged += new PropertyChangedEventHandler(_handler_PropertyChanged);

            _refreshTimer = new DispatcherTimer(DispatcherPriority.Background)
            {
                Interval = new TimeSpan(0, 0, 3),
            };
            _refreshTimer.Tick += new EventHandler(_refreshTimer_Tick);
            _refreshTimer.Start();
        }

        void _handler_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnPropertyChanged(e.PropertyName);
        }

        private void _refreshTimer_Tick(object sender, EventArgs e)
        {
            OnPropertyChanged((string)null);
        }

        public int FramesProcessed
        {
            get { return _handler.FramesProcessed; }
        }

        public bool VideoCaptureProcessing
        {
            get { return _handler.VideoCaptureProcessing; }
        }

        public int MinimumProcessSleepTime
        {
            get { return _handler.MinimumProcessSleepTime; }
            set
            {
                if (_handler.MinimumProcessSleepTime != value)
                {
                    _handler.MinimumProcessSleepTime = value;
                }
            }
        }

        public double FPS
        {
            get { return _handler.FPS; }
        }

        public bool CurrentlyProcessing { get { return _handler.CurrentlyProcessing; } }

        public double CurrentTimeBetweenFrames { get { return _handler.CurrentTimeBetweenFrames; } }

        public int WaitForNextImageSleepTime { get { return _handler.WaitForNextImageSleepTime; } }

        public int WaitForNextImageSleepTimeOverride { get { return _handler.WaitForNextImageSleepTimeOverride; } }

        public int SleepTimeRetyCheckCameraStarted
        {
            get { return _handler.SleepTimeRetyCheckCameraStarted; }
        }

        public List<RotateFlipType> AllFrameImageFlipTypes
        {
            get
            {
                List<RotateFlipType> results = new List<RotateFlipType>();
                var vals = Enum.GetValues(typeof(RotateFlipType));
                foreach (var val in vals)
                {
                    results.Add((RotateFlipType)val);
                }
                return results;
            }
        }

        public RotateFlipType FrameImageFlipType
        {
            get { return _handler.FrameImageFlipType; }
            set
            {
                if (_handler.FrameImageFlipType != value)
                {
                    _handler.FrameImageFlipType = value;
                }
            }
        }

        public DateTime LastProcessedFrame
        {
            get { return _handler.LastProcessedFrame; }
        }

        public bool WantExit
        {
            get { return _handler.WantExit; }
            set
            {
                if (_handler.WantExit != value)
                {
                    _handler.WantExit = value;
                }
            }
        }

        public ThreadPriority ProcessingThreadPriority
        {
            get { return _handler.ProcessingThreadPriority; }
            set
            {
                if (_handler.ProcessingThreadPriority != value)
                {
                    _handler.ProcessingThreadPriority = value;
                }
            }
        }
    }
}
